
DOM = (document.getElementById) ? 1 : 0;
NS4 = (document.layers) ? 1 : 0;
Konqueror = (navigator.userAgent.indexOf("Konqueror") > -1) ? 1 : 0;
Konqueror22 = (navigator.userAgent.indexOf("Konqueror 2.2") > -1 || navigator.userAgent.indexOf("Konqueror/2.2") > -1) ? 1 : 0;
Opera = (navigator.userAgent.indexOf("Opera") > -1) ? 1 : 0;
Opera5 = (navigator.userAgent.indexOf("Opera 5") > -1 || navigator.userAgent.indexOf("Opera/5") > -1) ? 1 : 0;
Opera6 = (navigator.userAgent.indexOf("Opera 6") > -1 || navigator.userAgent.indexOf("Opera/6") > -1) ? 1 : 0;
Opera56 = Opera5 || Opera6;
IE = (navigator.userAgent.indexOf("MSIE") > -1) ? 1 : 0;
IE = IE && !Opera;
IE5 = IE && DOM;
IE4 = (document.all) ? 1 : 0;
IE4 = IE4 && IE && !DOM;
var ppcXC = null;
var ppcYC = null;
var lastLayer = null;


if (!IE4 && !IE5) {
	document.captureEvents(Event.MOUSEDOWN|Event.MOUSEUP);
	document.onmousedown = recordXY;
	document.onmouseup   = confirmXY;
}

function recordXY(e) {
	if (!IE4 && !IE5) {
		ppcXC = e.clientX;
		ppcYC = e.clientY;
		document.routeEvent(e);
	}
}

function confirmXY(e) {
	if (!IE4 && !IE5) {
		ppcXC = (ppcXC == e.clientX) ? e.clientX : null;
		ppcYC = (ppcYC == e.clientY) ? e.clientY : null;
		document.routeEvent(e);
	}
}

function getCalendar(layer_id, str_target, str_date, str_type) {
	if ((!DOM || Opera56 || Konqueror22) && !IE4) {
		return;
	}

	if (lastLayer) hideCalendar(lastLayer);

	if(!show_calendar(str_target, str_date, str_type, layer_id))return;

	if (!IE4 && !IE5) {
		srcLayer = document.getElementById(layer_id);
		srcLayer.style.left = ((document.body.scrollLeft + ppcXC + 200) > document.body.clientWidth ? document.body.clientWidth - 200 : document.body.scrollLeft + ppcXC);
		srcLayer.style.top  = document.body.scrollTop + ppcYC;
	}
	else {
		srcLayer = document.all(layer_id);
		srcLayer.style.left = ((document.body.scrollLeft + event.clientX + 200) > document.body.clientWidth ? document.body.clientWidth - 200 : document.body.scrollLeft + event.clientX);
		srcLayer.style.top  = document.body.scrollTop + event.clientY;
	}

	srcLayer.style.visibility = "visible";

	lastLayer = layer_id;
}

function hideCalendar(layer_id) {
	if (!IE4 && !IE5) srcLayer = document.getElementById(layer_id);
	else srcLayer = document.all(layer_id);

	srcLayer.style.visibility = "hidden";
}

function show_calendar(str_target, str_date, str_type, layer_id) {
	var arr_months = ["Январь", "Февраль", "Март", "Апрель", "Май", "Июнь",
		"Июль", "Август", "Сентябрь", "Октябрь", "Ноябрь", "Декабрь"];
	var week_days = ["Вс", "Пн", "Вт", "Ср", "Чт", "Пт", "Сб"];
	var n_weekstart = 1; 

	if (str_date != null && str_date != "") {
		if (str2dt2(str_date, str_type) == false) {
		 	alert("Неверный формат даты"+(str_type == 1 ? "/времени" : "")+" (DD.MM.YYYY"+(str_type == 1 ? " HH:MM" : "")+")");
			return false;
		}
	}

	if (!IE4 && !IE5) {
		hrsEl = "document.getElementById('hours_"+layer_id+"').";
		minEl = "document.getElementById('minutes_"+layer_id+"').";
	}
	else {
		hrsEl = "document.all.hours_"+layer_id+".";
		minEl = "document.all.minutes_"+layer_id+".";
	}

	var dt_date = (str_date == null || str_date =="" ?  new Date() : str2dt2(str_date, str_type));

	var dt_prev_year = new Date(dt_date);
	dt_prev_year.setMonth(dt_date.getMonth()-12);

	var dt_next_year = new Date(dt_date);
	dt_next_year.setMonth(dt_date.getMonth()+12);

	var dt_prev_month = new Date(dt_date);
	dt_prev_month.setMonth(dt_date.getMonth()-1);
	if (dt_date.getMonth()%12 != (dt_prev_month.getMonth()+1)%12) {
		dt_prev_month.setMonth(dt_date.getMonth());
		dt_prev_month.setDate(0);
	}
	var dt_next_month = new Date(dt_date);
	dt_next_month.setMonth(dt_date.getMonth()+1);
	if ((dt_date.getMonth() + 1)%12 != dt_next_month.getMonth()%12)
		dt_next_month.setDate(0);
	
	var dt_firstday = new Date(dt_date);
	dt_firstday.setDate(1);
	dt_firstday.setDate(1-(7+dt_firstday.getDay()-n_weekstart)%7);
	var dt_lastday = new Date(dt_next_month);
	dt_lastday.setDate(0);
	
	var str_buffer = new String (
		"<table align=center cellspacing=\"1\" cellpadding=\"1\" border=\"0\" width=196><tr><td bgcolor=\"#000000\">\n"+
		"<table align=center cellspacing=\"0\" cellpadding=\"1\" border=\"0\" width=196 bgcolor=\"#ffffff\">\n"+
		"<tr bgcolor=\"#b8e5b7\">\n"+
		"<td align=center><a title=\"Предыдущий год\" href=\"javascript:void(0);\" onClick=\"show_calendar('"+str_target+"', '"+dt2dtstr2(dt_prev_year)+
		(str_type == 1 ? " '+"+hrsEl+"options["+hrsEl+"selectedIndex].value+':'+"+minEl+"options["+minEl+"selectedIndex].value, 1" : "', 0")+
		", '" + layer_id + "');\" style=\"text-decoration: none; color:#000000; font-size:9pt; font-family: arial\" href=\"javascript:void(0);\"><b>&lt;&lt;</b></a>&nbsp;&nbsp;<a title=\"Предыдущий месяц\" style=\"text-decoration: none; color:#000000; font-size:9pt; font-family: arial\" href=\"javascript:void(0);\" onClick=\"show_calendar('"+
		str_target+"', '"+ dt2dtstr2(dt_prev_month)+
		(str_type == 1 ? " '+"+hrsEl+"options["+hrsEl+"selectedIndex].value+':'+"+minEl+"options["+minEl+"selectedIndex].value, 1" : "', 0")+
		", '" + layer_id + "');\"><b>&lt;</b></a></td>"+

		"<td colspan=\"4\" align=center><font style=\"color:#000000; font-size:9pt; font-family: arial\">"+arr_months[dt_date.getMonth()]+" "+dt_date.getFullYear()+"</font></td>\n"+

		"<td><a title=\"Следующий месяц\" style=\"text-decoration: none; color:#000000; font-size:9pt; font-family: arial\" href=\"javascript:void(0);\" onClick=\"show_calendar('"+
		str_target+"', '"+dt2dtstr2(dt_next_month)+
		(str_type == 1 ? " '+"+hrsEl+"options["+hrsEl+"selectedIndex].value+':'+"+minEl+"options["+minEl+"selectedIndex].value, 1" : "', 0")+
		", '" + layer_id + "');\"><b>&gt;</b></a>&nbsp;&nbsp;<a title=\"Следующий год\" href=\"javascript:void(0);\" onClick=\"show_calendar('"+str_target+"', '"+dt2dtstr2(dt_next_year)+
		(str_type == 1 ? " '+"+hrsEl+"options["+hrsEl+"selectedIndex].value+':'+"+minEl+"options["+minEl+"selectedIndex].value, 1" : "', 0")+
		", '" + layer_id + "');\" style=\"text-decoration: none; color:#000000; font-size:9pt; font-family: arial\" href=\"javascript:void(0);\"><b>&gt;&gt;</b></a></td>\n"+

		"<td align=center>&nbsp;<a title=\"Закрыть\" style=\"text-decoration: none; color:#000000; font-size:9pt; font-family: arial\" href=\"javascript:void(0);\" onClick=\"hideCalendar('" + layer_id + "');\"><b>x</b></a>&nbsp;</td>\n"+
		"</tr>\n"
	);

	var dt_current_day = new Date(dt_firstday);
	str_buffer += "<tr bgcolor=\"#f5f5f5\">\n";
	for (var n=0; n<7; n++)
		str_buffer += "<td align=center width=28><font style=\"color:#b22222; font-size:9pt; font-family: arial\">"+
		week_days[(n_weekstart+n)%7]+"</font></td>\n";
	str_buffer += "</tr>\n";
	while (dt_current_day.getMonth() == dt_date.getMonth() ||
		dt_current_day.getMonth() == dt_firstday.getMonth()) {
		str_buffer += "<tr>\n";
		for (var n_current_wday=0; n_current_wday<7; n_current_wday++) {
				if (dt_current_day.getDate() == dt_date.getDate() &&
					dt_current_day.getMonth() == dt_date.getMonth())
					
					str_buffer += "	<td  align=\"center\" bgcolor=\"#b8e5b7\">";
				else if (dt_current_day.getDay() == 0 || dt_current_day.getDay() == 6)
					
					str_buffer += "	<td align=\"center\">";
				else
					
					str_buffer += "	<td align=\"center\">";

				if (dt_current_day.getMonth() == dt_date.getMonth())
					
					str_buffer += "<a style=\"color:#007a50; font-size:9pt; font-family: arial; text-decoration: none;\" href=\"javascript:void(0);\" onClick=\""+str_target+
					".value='"+dt2dtstr2(dt_current_day)+
					(str_type == 1 ? " '+"+hrsEl+"options["+hrsEl+"selectedIndex].value+':'+"+minEl+"options["+minEl+"selectedIndex].value" : "'")+
					"; hideCalendar('" + layer_id + "');\">";
				else 
					
					str_buffer += "<a style=\"color:#ababab; font-size:9pt; font-family: arial; text-decoration: none;\" href=\"javascript:void(0);\" onClick=\""+str_target+
					".value='"+dt2dtstr2(dt_current_day)+
					(str_type == 1 ? " '+"+hrsEl+"options["+hrsEl+"selectedIndex].value+':'+"+minEl+"options["+minEl+"selectedIndex].value" : "'")+
					"; hideCalendar('" + layer_id + "');\">";
				str_buffer += dt_current_day.getDate()+"</a></td>\n";
				dt_current_day.setDate(dt_current_day.getDate()+1);
		}
		
		str_buffer += "</tr>\n";
	}

	// форма "часы/минуты"
	if (str_type == 1) {
		str_buffer +=
			"<tr>\n\t<td colspan=\"7\"><hr size=\"1\"><\/td>\n<\/tr>\n" +
			"<tr>\n\t\n\t<td colspan=\"7\" align=\"center\">" +
			"<font style=\"color:#b22222; font-size:9pt; font-family: arial\">время<\/font>:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" +
			"<select name=\"hours_"+layer_id+"\" id=\"hours_"+layer_id+"\">";
		for (hrs = 0; hrs < 24; hrs++) {
			str_buffer +=
				"<option value=\"" + (hrs.toString().length == 1 ? "0" : "") + hrs +
				"\"" + (hrs == dt_date.getHours() ? " selected" : "") + ">" +
				(hrs.toString().length == 1 ? "0" : "") + hrs;
		}
		str_buffer +=
			"<\/select> ч. " +
			"<select name=\"minutes_"+layer_id+"\" id=\"minutes_"+layer_id+"\">";
		for (mns = 0; mns < 60; mns++) {
			str_buffer +=
				"<option value=\"" + (mns.toString().length == 1 ? "0" : "") + mns +
				"\"" + (mns == dt_date.getMinutes() ? " selected" : "") + ">" +
				(mns.toString().length == 1 ? "0" : "")+mns;
		}
		str_buffer +=
			"<\/select> мин." +
			"<\/td>\n\t\n<\/tr>\n";
	}

	str_buffer +=
		"</table>\n</td></tr></table>\n";

	if ((!DOM || Opera56 || Konqueror22) && !IE4) {
		return;
	}

	if (!IE4 && !IE5) srcLayer = document.getElementById(layer_id);
	else srcLayer = document.all(layer_id);

	srcLayer.innerHTML = str_buffer;
	return true;
}

function str2dt2 (str_date, str_type) {
	var re_date;
	if (str_type == 1) {
		re_date = /^(\d+)\.(\d+)\.(\d+) (\d+)\:(\d+)$/;
	}
	else {
		re_date = /^(\d+)\.(\d+)\.(\d+)$/;
	}
	if (!re_date.exec(str_date)) {
		return (false);
	} else {
		if (str_type == 1) {
			var arrDT = new Array();
			var arrD = new Array();
			var arrT = new Array();
			var m, d, Y, HH, mm;
			arrDT = str_date.split(" ");
			arrD = arrDT[0].split(".");
			arrT = arrDT[1].split(":");
			d = parseInt(correct_number(arrD[0]));
			m = parseInt(correct_number(arrD[1])) - 1;
			Y = parseInt(arrD[2]);
			HH = parseInt(correct_number(arrT[0]));
			mm = parseInt(correct_number(arrT[1]));
			return (new Date(Y, m, d, HH, mm));
		}
		else {
			var arrD = new Array();
			var m, d, Y;
			arrD = str_date.split(".");
			d = parseInt(correct_number(arrD[0]));
			m = parseInt(correct_number(arrD[1])) - 1;
			Y = parseInt(arrD[2]);
			return (new Date(Y, m, d));
		}
		//return (new Date (RegExp.$3, RegExp.$2-1, RegExp.$1));
	}
}

function dt2dtstr2 (dt_date) {
var bday, bmonth;
bday = dt_date.getDate().toString();
bmonth = (dt_date.getMonth()+1).toString();

if (bmonth.length == 1) {
	bmonth = "0" + bmonth;
}
if (bday.length == 1) {
	bday = "0" + bday;
}

	return (new String (
			bday+"."+bmonth+"."+dt_date.getFullYear()));
}

function correct_number(strIn) {
	var res;
	if (strIn.length == 2 && (strIn.substr(0, 1) == '0')) {
		res = strIn.substr(1, 1);
	} else {
		res = strIn;
	}
	return (res);
}

